package com.yanqu.road.server.manger.config;

import com.yanqu.road.dao.impl.config.MallRechargeConfigDaoImpl;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.config.goods.MallRechargeConfig;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MallRechargeConfigMgr extends TempMgr {

    /**
     * rechargeMap
     */
    private static Map<Integer, MallRechargeConfig> rechargeMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        rechargeMap = new MallRechargeConfigDaoImpl().getMallRechargeMap();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    /**
     * 获取recharge配置
     * @param id
     * @return
     */
    public static MallRechargeConfig getRechargeConfig(int id){
        return rechargeMap.get(id);
    }

    /**
     * 根据mall获取充值配置
     * @param mallInfo
     * @return
     */
    public static MallRechargeConfig getRechargeConfigByMall(MallInfo mallInfo){
        if (mallInfo == null || mallInfo.getRechargeId() <= 0) {
            return null;
        }
    	return rechargeMap.get(mallInfo.getRechargeId());
    }

    /**
     * 获取mall的param值
     * @param mallInfo
     * @return
     */
    public static long getParamValue(MallInfo mallInfo) {
        if (mallInfo == null) {
            return 0;
        }
        if (mallInfo.getActivityId() > 0) {
            long paramValue;
            //活动的mall，读recharge配置
            MallRechargeConfig rechargeConfig = getRechargeConfig(mallInfo.getRechargeId());
            if (rechargeConfig == null) {
                //没获取到，取mall上的
                paramValue = StringUtils.string2longSafe(mallInfo.getParam(), 0);
            } else {
                paramValue = Long.parseLong(rechargeConfig.getParam());
            }
            return paramValue;
        } else {
            //非活动的mall，直接读mall配置
            return StringUtils.string2longSafe(mallInfo.getParam(), 0);
        }
    }

    /**
     * 获取mall的price值
     * @param mallInfo
     * @return
     */
    public static long getPrice(MallInfo mallInfo) {
        if (mallInfo == null) {
            return 0;
        }
        if (mallInfo.getActivityId() > 0) {
            long price;
            //活动的mall，读recharge配置
            MallRechargeConfig rechargeConfig = getRechargeConfig(mallInfo.getRechargeId());
            if (rechargeConfig == null) {
                //没获取到，取mall上的
                price = StringUtils.string2longSafe(mallInfo.getPrice(), 0);
            } else {
                price = rechargeConfig.getPrice();
            }
            return price;
        } else {
            //非活动的mall，直接读mall配置
            return StringUtils.string2longSafe(mallInfo.getPrice(), 0);
        }
    }

    /**
     * 获取mall的price字符串
     * @param mallInfo
     * @return
     */
    public static String getPriceStr(MallInfo mallInfo) {
        if (mallInfo == null) {
            return "";
        }
        if (mallInfo.getActivityId() > 0) {
            String price;
            //活动的mall，读recharge配置
            MallRechargeConfig rechargeConfig = getRechargeConfig(mallInfo.getRechargeId());
            if (rechargeConfig == null) {
                //没获取到，取mall上的
                price = mallInfo.getPrice();
            } else {
                price = String.valueOf(rechargeConfig.getPrice());
            }
            return price;
        } else {
            //非活动的mall，直接读mall配置
            return mallInfo.getPrice();
        }
    }

    /**
     * 获取mall的returnIngots值
     * @param mallInfo
     * @return
     */
    public static long getReturnIngots(MallInfo mallInfo) {
        if (mallInfo == null) {
            return 0;
        }
        if (mallInfo.getActivityId() > 0) {
            long returnIngots;
            //活动的mall，读recharge配置
            MallRechargeConfig rechargeConfig = getRechargeConfig(mallInfo.getRechargeId());
            if (rechargeConfig == null) {
                //没获取到，取mall上的
                returnIngots = mallInfo.getReturnIngots();
            } else {
                returnIngots = rechargeConfig.getReturnIngots();
            }
            return returnIngots;
        } else {
            //非活动的mall，直接读mall配置
            return mallInfo.getReturnIngots();
        }
    }
}
